﻿import QtQuick
import QtQuick.Controls

Button{
    id: btn
//    width: 25
//    height: 25

    //外部可访问属性
    property alias backgroundColor: backRect.color
    property string hoveredText
    property int cursorShape: Qt.PointingHandCursor
    property alias tipText: tip.text
    property int hoverTime: 500
    property alias radius: backRect.radius
    readonly property bool isHovered: hoverLoader.active ?  hoverLoader.item.hovered:false
//    property alias textOverrided: label.text
//    property alias textColor: label.baseColor
//    property color textHoveredColor
//    property alias fontSize: label.font.pixelSize
//    property alias textHorizontalCenterOffset: label.anchors.horizontalCenterOffset
//    property alias textVerticalCenterOffset: label.anchors.verticalCenterOffset

    signal hoverTimeOver(real x, real y)


    property real tipX: -1
    property real tipY: -1


    hoverEnabled: false //关闭悬浮效果

    //开关接口
    property bool needHoverTimer : false

//    contentItem:Text
//    {   id: label
//        anchors.horizontalCenter:btn.horizontalCenter
//        anchors.verticalCenter:btn.verticalCenter
//        property color baseColor: Qt.lighter("#666",0.95)
//        color: {
//            if(btn.isHovered){
//                if(btn.textHoveredColor){
//                    return btn.textHoveredColor
//                }
//                else{
//                    return Qt.lighter(baseColor,0.05)
//                }
//            }else{
//                return baseColor
//            }

//        }
//    }

    background: Rectangle{
        id:backRect
        anchors.fill: parent
        color: "transparent"

    }

    Loader{
        id: hoverLoader
        active: needHoverTimer
        anchors.fill: btn
        signal hoveredChanged(bool hovered)
        signal mouseExit()
        property int pointerShape: btn.cursorShape

        sourceComponent: Item{
            id: hoverRoot
            anchors.fill: parent

            readonly property var point: mouse.stillPoint
            readonly property bool hovered: mouse.hovered

            HoverHandler
            {
                id:mouse
                target: null

                property var stillPoint: mouse.point.position
                acceptedButtons: Qt.NoButton

                onHoveredChanged: {
                    hoverRoot.parent.hoveredChanged(hovered)
                }
                cursorShape: hoverRoot.parent.pointerShape
        }
        }

    }

        Loader {
            id: dwellLoader
            active: needHoverTimer
            property int interval: btn.hoverTime
            signal timeOver()
            sourceComponent: Item {
                id: timerRoot

                Timer {
                    id: timer
                    interval: timerRoot.parent.interval
                    repeat: false
                    running: false     // 创建待计时
                    onTriggered: {
                        timerRoot.parent.timeOver()
                    }
                }
                function restart(){
                    timer.restart()
                }
                function stop(){
                    timer.stop()
                }
            }

        }

        Connections{
            target: dwellLoader
            //静止满一秒
            function onTimeOver(){
                hoverTimeOver(hoverLoader.item.point.x,hoverLoader.item.point.y)
                tipX = hoverLoader.item.point.x + 12
                tipY = hoverLoader.item.point.y + 12
            }
        }

        Connections{
            target:hoverLoader
            function onHoveredChanged(hovered){
                if(hovered){
                    dwellLoader.item.restart()
                }else{
                    dwellLoader.item.stop()
                    hoverLoader.mouseExit()
                }
            }

            function onMouseExit(){
                tip.visible = false
            }
        }

        Connections{
            target:hoverLoader.item
            ignoreUnknownSignals: true
                function onPointChanged() {
                    if (hoverLoader.item && hoverLoader.item.hovered && dwellLoader.item) {
                        dwellLoader.item.restart()
                    }
                }

        }

        onHoverTimeOver: {
            tip.visible = true;
        }

    function closeTip(){
        tip.visible = false
    }

    onPressed: {
        if(dwellLoader.active){
            dwellLoader.item.stop()
            tip.visible =  false
        }
    }

    onReleased: {
        if(dwellLoader.active){
            dwellLoader.item.restart()
        }
    }


    ToolTip{
        id: tip
        visible: false
        x: tipX
        y: tipY
    }



}
